package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IEmpSocPlanItemDAO;
import org.jsola.hr.dto.EmpSocPlanItemAddDTO;
import org.jsola.hr.dto.EmpSocPlanItemUpdateDTO;
import org.jsola.hr.entity.EmpSocPlanDO;
import org.jsola.hr.entity.EmpSocPlanItemDO;
import org.jsola.hr.query.EmpSocPlanItemQuery;
import org.jsola.hr.service.IEmpSocPlanItemService;
import org.jsola.hr.vo.EmpSocPlanItemListVO;
import org.jsola.hr.vo.EmpSocPlanItemVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 增减员计划子项表
 *
 * @author zhr
 */
@Service("hrEmpSocPlanItemServiceImpl")
public class EmpSocPlanItemServiceImpl implements IEmpSocPlanItemService {

    @Autowired
    private IEmpSocPlanItemDAO empSocPlanItemDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpSocPlanItemVO save(EmpSocPlanItemAddDTO empSocPlanItemAddDTO, TokenUser tokenUser) {
        // 转DO
        EmpSocPlanItemDO empSocPlanItemDO = empSocPlanItemAddDTO.to(EmpSocPlanItemDO.class);
        // 保存
        empSocPlanItemDO = save(empSocPlanItemDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return empSocPlanItemDO.to(EmpSocPlanItemVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(EmpSocPlanItemUpdateDTO empSocPlanItemUpdateDTO, TokenUser tokenUser) {
        //转DO
        EmpSocPlanItemDO empSocPlanItemDO = empSocPlanItemUpdateDTO.to(EmpSocPlanItemDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(empSocPlanItemDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... empSocPlanItemIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) empSocPlanItemIds);
    }


    @Override
    public EmpSocPlanItemVO selectById(String empSocPlanItemId, String siteId) {
        EmpSocPlanItemDO empSocPlanItemDO = selectDOById(empSocPlanItemId, siteId);
        if (empSocPlanItemDO == null) {
            return null;
        }
        return empSocPlanItemDO.to(EmpSocPlanItemVO.class);
    }

    @Override
    public List<EmpSocPlanItemListVO> select(EmpSocPlanItemQuery empSocPlanItemQuery, String siteId) {
        List<EmpSocPlanItemDO> empSocPlanItemDOList = selectDO(empSocPlanItemQuery, siteId);
        if (CollectionUtils.isEmpty(empSocPlanItemDOList)) {
            return empSocPlanItemDOList == null ? null : new ArrayList<>();
        }
        return empSocPlanItemDOList.stream()
                .map(empSocPlanItemDO -> empSocPlanItemDO.to(EmpSocPlanItemListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(EmpSocPlanItemQuery empSocPlanItemQuery, String siteId) {
        Example example = buildExample(empSocPlanItemQuery, siteId);
        return empSocPlanItemDAO.selectCountByExample(example);
    }

    @Override
    public Page<EmpSocPlanItemListVO> selectPage(EmpSocPlanItemQuery empSocPlanItemQuery, String siteId) {
        Example example = buildExample(empSocPlanItemQuery, siteId);
        Page<EmpSocPlanItemDO> page = empSocPlanItemDAO.selectPageByExample(example,
                empSocPlanItemQuery.getPageNo(),
                empSocPlanItemQuery.getPageSize());

        return page.to(EmpSocPlanItemListVO.class);
    }

    @Override
    public EmpSocPlanItemDO selectDOById(String empSocPlanItemId, String siteId) {
        return listById(empSocPlanItemId, siteId);
    }

    @Override
    public List<EmpSocPlanItemDO> selectDO(EmpSocPlanItemQuery empSocPlanItemQuery, String siteId) {
        Example example = buildExample(empSocPlanItemQuery, siteId);
        return empSocPlanItemDAO.selectByExample(example);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public void bachSave(List<EmpSocPlanItemDO> empSocPlanItemList) {
        // 批量保存
        empSocPlanItemDAO.insertListAndSetId(empSocPlanItemList);
    }

    @Override
    public List<EmpSocPlanItemDO> selectByPlanId(String planId, TokenUser tokenUser) {
        Example example = new Example(EmpSocPlanItemDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", tokenUser.getSiteId())
                .andEqualTo("planId", planId);
        return empSocPlanItemDAO.selectByExample(example);
    }

    @Override
    public Map<String, List<EmpSocPlanItemDO>> selectByPlanIdList(List<String> planIdList, TokenUser tokenUser) {
        Example example = new Example(EmpSocPlanItemDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", tokenUser.getSiteId())
                .andIn("planId", planIdList);
        List<EmpSocPlanItemDO> empSocPlanItemList = empSocPlanItemDAO.selectByExample(example);
        return empSocPlanItemList.stream()
                .collect(Collectors.groupingBy(EmpSocPlanItemDO::getPlanId));
    }

    @Override
    public List<EmpSocPlanItemVO> selectByPlanIds(List<String> planIdList, TokenUser tokenUser) {
        if(CollectionUtils.isEmpty(planIdList)){
            return new ArrayList<>();
        }
        Example example = new Example(EmpSocPlanItemDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", tokenUser.getSiteId())
                .andIn("planId", planIdList);
        List<EmpSocPlanItemDO> empSocPlanItemList = empSocPlanItemDAO.selectByExample(example);
        return empSocPlanItemList.stream().map(item->item.to(EmpSocPlanItemVO.class)).collect(Collectors.toList());
    }

    @Override
    public List<EmpSocPlanItemDO> selectBySite(TokenUser tokenUser) {
        Example example = new Example(EmpSocPlanItemDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", tokenUser.getSiteId());
        return empSocPlanItemDAO.selectByExample(example);
    }

    @Override
    public List<EmpSocPlanItemVO> selectByCompanyId(String companyId, TokenUser tokenUser){
        return empSocPlanItemDAO.selectByCompanyId(companyId, tokenUser.getSiteId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public void deleteByPlanId(String planId, TokenUser tokenUser) {
        Example example = new Example(EmpSocPlanItemDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", tokenUser.getSiteId())
                .andEqualTo("planId", planId);
        empSocPlanItemDAO.deleteByExample(example);
    }

    /**
     * 根据查询参数，构建example
     *
     * @param empSocPlanItemQuery 查询参数
     * @param siteId              所属站点id
     * @return example
     */
    private Example buildExample(EmpSocPlanItemQuery empSocPlanItemQuery, String siteId) {
        Example example = new Example(EmpSocPlanItemDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example, empSocPlanItemQuery.getOrders());
        return example;
    }
}




